package com.supercube.wechat.controller;

import com.supercube.common.controller.BaseController;
import com.supercube.common.controller.IPageDefine;
import com.supercube.common.utils.PageUtils;
import com.supercube.common.utils.Query;
import com.supercube.common.utils.R;
import com.supercube.wechat.domain.RecommendCourseDO;
import com.supercube.wechat.service.RecommendCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * 小程序推荐课程表
 * @author djzhao
 * @date 2019-10-30 22:55:56
 */
 
@Controller
@RequestMapping("/wechat/recommendCourse2")
public class RecommendCourseController2 extends BaseController {
	@Autowired
	private RecommendCourseService recommendCourseService;
	
	@GetMapping()
	// @RequiresPermissions("wechat:recommendCourse:recommendCourse")
	String RecommendCourse(){
	    return "wechat/recommendCourse/recommendCourse_list2";
	}
	
	/**
	 * 分页列表查询
	 */
	@ResponseBody
	@GetMapping("/listPage")
	// @RequiresPermissions("wechat:recommendCourse:recommendCourse")
	public PageUtils list(@RequestParam Map<String, Object> params){
		// 名师进修课程
		params.put("type", 1);
		//根据分页参数(格式：{limit=10, offset=0} )，然后进行分页查询
        return getPageList(params, new IPageDefine() {
			@Override
			public List<?> getPageRows(Query query){
				return  recommendCourseService.list(query);
			}
        });
	}
	
	/**
	 * 跳转到新增页面
	 */
	@GetMapping("/add")
	// @RequiresPermissions("wechat:recommendCourse:add")
    ModelAndView add(){
	    ModelAndView mv = new ModelAndView();
		mv.addObject("recommendCourse", new RecommendCourseDO());
		mv.setViewName("wechat/recommendCourse/recommendCourse_edit2");
		return mv;
	}
	
	/**
	 * 跳转到修改页面
	 */
	@GetMapping("/edit/{id}")
	// @RequiresPermissions("wechat:recommendCourse:edit")
    ModelAndView edit(@PathVariable("id") Integer id){
	    ModelAndView mv = new ModelAndView();
		RecommendCourseDO recommendCourse = recommendCourseService.get(id);
		mv.addObject("recommendCourse", recommendCourse);
		mv.setViewName("wechat/recommendCourse/recommendCourse_edit2");
		return mv;
	}
	
	/**
	 * 新增方法
	 */
	@ResponseBody
	@PostMapping("/save")
	// @RequiresPermissions("wechat:recommendCourse:add")
	public R save( RecommendCourseDO recommendCourse){
		if(recommendCourseService.save(recommendCourse)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改方法
	 */
	@ResponseBody
	@RequestMapping("/update")
	// @RequiresPermissions("wechat:recommendCourse:edit")
	public R update( RecommendCourseDO recommendCourse){
		recommendCourseService.update(recommendCourse);
		return R.ok();
	}
	
	/**
	 * 单条删除方法
	 */
	@PostMapping( "/remove")
	@ResponseBody
	// @RequiresPermissions("wechat:recommendCourse:remove")
	public R remove( Integer id){
		if(recommendCourseService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 批量删除方法
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	// @RequiresPermissions("wechat:recommendCourse:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		recommendCourseService.batchRemove(ids);
		return R.ok();
	}
	
}
